﻿using Microsoft.Extensions.Configuration;
using MiniFox.Configurations;
using MiniFox.Web;
using System.Collections.ObjectModel;

namespace MiniFox.Platform.Configurations
{
    /// <summary>
    /// 
    /// </summary>
    [Section(ELEMENT_NAME)]
    public class WebApiClientConfiguration : KeyedCollection<string, WebApiClientSetting>
    {
        internal const string ELEMENT_NAME = "minifox:webApiClients";
        /// <summary>
        /// 
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        protected override string GetKeyForItem(WebApiClientSetting item)
        {
            return item.Name;
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public class WebApiClientSetting
    {
        const string ISSUE_TOKEN_URL = "/jwt/issue";
        /// <summary>
        /// 
        /// </summary>
        [ConfigurationKeyName("name")]
        public string Name
        {
            get; set;
        }
        /// <summary>
        /// 验证主机，必须设置为应用程序虚拟路径(例如: http://www.xxxxxx.com:8888 ,  http://www.xxxxxx.com:8080/passport , http://passport.xxxxxx.com)
        /// </summary>
        [ConfigurationKeyName("host")]
        public string Host
        {
            get; set;
        }
        /// <summary>
        /// 
        /// </summary>
        [ConfigurationKeyName("accessTokenUrl")]
        public string AccessTokenUrl
        {
            get; set;
        } = ISSUE_TOKEN_URL;
        /// <summary>
        /// token 刷新间隔(秒),默认值3600秒(1小时)
        /// </summary>
        [ConfigurationKeyName("renewInterval")]
        public int RenewInterval
        {
            get; set;
        }
        /// <summary>
        /// 机构编号
        /// </summary>
        [ConfigurationKeyName("orgId")]
        public string OrganizationID
        {
            get; set;
        }
        /// <summary>
        /// 应用编号
        /// </summary>
        [ConfigurationKeyName("appId")]
        public string ApplicationID
        {
            get; set;
        }
        /// <summary>
        /// 安全密钥
        /// </summary>
        [ConfigurationKeyName("secretKey")]
        public string SecretKey
        {
            get; set;
        }
        /// <summary>
        /// 是否允许使用缓存
        /// </summary>
        [ConfigurationKeyName("enableCache")]
        public bool EnableCache
        {
            get; set;
        }
        /// <summary>
        /// 获取完整的access token地址url, Host + AccessTokenUrl (例如： http://passport.xxxxxx.xxx/jwt/issue)
        /// </summary>
        public string FullAccessTokenUrl
        {
            get
            {
                string url = this.AccessTokenUrl;

                url = WebUtil.BuildFullUrl(this.Host, url);
                return url;
            }
        }
    }
}
